package com.pateo.retail.canal.handler.es;

import com.pateo.retail.canal.es.EsService;
import com.pateo.retail.canal.es.vo.EsAddReqVo;
import com.pateo.retail.canal.es.vo.EsUpsertReqVo;
import com.pateo.retail.canal.model.CustomerInfoFieldValue;
import com.pateo.retail.canal.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Component
@CanalTable(value = "customer_info_field_value")
public class CustomerInfoFieldValueHandler implements EntryHandler<CustomerInfoFieldValue> {


    private Logger logger = LoggerFactory.getLogger(CustomerActionLogHandler.class);


    @Autowired
    private EsService esService;


    @Override
    public void insert(CustomerInfoFieldValue customerInfoFieldValue) {
        logger.info("insert message  {}", customerInfoFieldValue);

        if (StringUtils.isEmpty(customerInfoFieldValue.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", customerInfoFieldValue);
            return;
        }

        EsUpsertReqVo reqVo = new EsUpsertReqVo();
        reqVo.setData(customerInfoFieldValue);
        reqVo.setIndex(CommonUtils.getEsIndex(customerInfoFieldValue.getTenant_code()));
        reqVo.setId(customerInfoFieldValue.getCustomer_id() + "");
        esService.upsert(reqVo);


    }

    @Override
    public void update(CustomerInfoFieldValue before, CustomerInfoFieldValue after) {
        logger.info("update before {} ", before);
        logger.info("update after {}", after);

        if (StringUtils.isEmpty(after.getTenant_code())) {
            logger.info("数据异常tenantCode为null，不执行同步es同步逻辑-----{}", after);
            return;
        }

        EsUpsertReqVo reqVo = new EsUpsertReqVo();
        reqVo.setData(after);
        reqVo.setIndex(CommonUtils.getEsIndex(after.getTenant_code()));
        reqVo.setId(after.getCustomer_id() + "");
        esService.upsert(reqVo);
    }

    @Override
    public void delete(CustomerInfoFieldValue customerInfoFieldValue) {
        logger.info("delete  {}", customerInfoFieldValue);
    }
}
